layui.define(function (exports) {
    var admin = layui.admin
        , $ = layui.$
        , element = layui.element
        , laytpl = layui.laytpl
        , router = layui.router();

    var currentRouter = (function () {
        if (router.href == '/' || !router.href) {
            return '/'
        } else {
            return router.href.replace('/', '')
        }
    })();

    var menustr = ``;
    function bulidMoreMenu(data) {
        menustr += `<dl class="layui-nav-child">`
        data.forEach(item => {
            if (item.list && item.list.length != 0) {
                menustr += `
                <dd>
                    <a href="javascript:;">
                        ${item.title}
                        <i class="layui-icon layui-icon-down layui-nav-more"></i>
                    </a>`
                bulidMoreMenu(item.list)
                menustr += `</dd>`
            } else {
                menustr += `
                <dd data-jump=${item.jump}" class="${item.jump == currentRouter ? 'layui-this' : ''}">
                    <a href="javascript:;" lay-href="${item.jump ? item.jump : ''}">${item.title}</a>
                </dd> 
                `
            }
        })
        menustr += `</dl>`

    }
    function bulidMenuStr(data) {
        data.forEach(item => {
            if (item.list && item.list.length != 0) {
                menustr += `
                <li data-jump class="layui-nav-item">
                    <a href="javascript:;" lay-tips="${item.title}" lay-direction="2"> 
                        <i class="layui-icon ${item.icon}"></i> 
                        <cite>${item.title}</cite> 
                        <i class="layui-icon layui-icon-down layui-nav-more"></i>
                    </a>
                `
                bulidMoreMenu(item.list)
                menustr += `</li>`
            } else {
                menustr += `
                <li data-jump="${item.jump ? item.jump : ''}" class="layui-nav-item ${item.jump == currentRouter ? 'layui-this' : ''}"> 
                    <a href="javascript:;" lay-href="${item.jump}" lay-tips="${item.title}"  lay-direction="2"> 
                        <i class="layui-icon ${item.icon}"></i>
                        <cite>${item.title}</cite>
                    </a>
                </li>`
            }
        });
    }

    // 菜单数据
    var menuData = {
        "code": 0,
        "msg": "",
        "data": [{
            "title": "主页"
            , "icon": "layui-icon-home"
            , "jump": '/'
        },
        {
            "name": "home"
            , "title": "页面"
            , "icon": "layui-icon-template"
            , "list": [{
                "name": "page1"
                , "title": "查询页面"
                , "jump": "home/page1"
            }, {
                "name": "page2"
                , "title": "消息中心"
                , "jump": "home/page2"
            }]
        }]
    }
    bulidMenuStr(menuData.data);
    $('#LAY-system-side-menu').html(menustr)
    element.render('nav', 'layadmin-system-side-menu');
    $('#LAY-system-side-menu').find(`[lay-href="${currentRouter}"]`).parents('dd,li').addClass('layui-nav-itemed');
    // 用户信息数据
    var userifodata = {
        "code": 0
        , "msg": ""
        , "data": {
            "username": "zhaoxiaoming"
            , "sex": "男"
            , "role": 1
        }
    }
    var userTpl = document.getElementById('userifo').innerHTML;
    laytpl(userTpl).render(userifodata.data, function (str) {
        $('#userifo').parent().html(str)
    });

    // 消息数据获取
    var ifoData = {
        "code": 0
        , "msg": ""
        , "data": {
            "newmsg": 3
        }
    }
    var ifoTpl = document.getElementById('index_ifo').innerHTML;
    laytpl(ifoTpl).render(ifoData.data, function (str) {
        $('#index_ifo').parent().append(str);
        admin.events.logout = function () {
            admin.exit();
        };
    });
    exports('layout', {})
});